package com.f5.management.service.impl;

import com.f5.management.dao.RealEstateDAO;
import com.f5.management.entity.dto.RealEstateUserDto;
import com.f5.management.service.RealEstateService;
import com.f5.management.service.UserService2;
import com.f5.management.utils.PageResult;
import com.f5.management.utils.SerialUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service

public class RealEstateServiceImpl implements RealEstateService {
    @Autowired
    private RealEstateDAO realEstateDAO;
    @Autowired
    private UserService2 userService2;

    //
    //添加
    @Override
    @Transactional
    public int add(RealEstateUserDto realEstateUserDto) {
        //自动生成编号    B1001
        int num = realEstateDAO.count();
        //生成的 n + 1
        realEstateUserDto.setSerialNumber(SerialUtil.RESerial(num + 1));
        //添加房产数据
        realEstateDAO.add(realEstateUserDto);
        //查询添加的rid 封装到user表里
        int rid = realEstateDAO.selectId();
        if (rid > 0) {
            userService2.add(realEstateUserDto.setRid(rid));
        }
        return 1;
    }

    //分页查询 首页展示
    @Override
    public PageResult<List<RealEstateUserDto>> findByPage(int page, int size) {
        PageResult<List<RealEstateUserDto>> result = new PageResult<List<RealEstateUserDto>>();
        Page<RealEstateUserDto> startPage = PageHelper.startPage(page, size);
        List<RealEstateUserDto> realEstates = realEstateDAO.findAll();
        result.setCurrentPage(page);
        result.setSize(size);
        result.setTotal(startPage.getTotal());
        result.setTotalPage(startPage.getPages());
        result.setData(realEstates);
        return result;
    }

    //删除信息
    @Override
    public boolean delInfo(int id) {
        return realEstateDAO.delInfo(id) > 0 ? true : false;
    }

    //id查询数据进行修改
    @Override
    public RealEstateUserDto findById(int id) {
        return realEstateDAO.findById(id);
    }

    //更改信息
    @Override
    public boolean updateInfo(RealEstateUserDto realEstateUserDto) {
        userService2.update(realEstateUserDto);
        return realEstateDAO.updateInfo(realEstateUserDto) > 0 ? true : false;

    }

    //查询编号是否存在
    @Override
    public boolean selectNumber(String serialNumber) {
        String oldNum = serialNumber;
        String newNum = realEstateDAO.selectNumber(serialNumber);
        //查询编号是否存在
        if (!oldNum.equals(newNum)) {
            return true;
        } else {
            return false;
        }
    }

    //通过编号查询数据
    @Override
    public PageResult<List<RealEstateUserDto>> findByNumber(String serialNumber, int rid, int page, int size) {
        PageResult<List<RealEstateUserDto>> result = new PageResult<List<RealEstateUserDto>>();
        Page<RealEstateUserDto> pageHelper = PageHelper.startPage(page, size);
        List<RealEstateUserDto> realEstates = realEstateDAO.findByNumber(serialNumber,rid);
        result.setCurrentPage(page);
        result.setSize(size);
        result.setTotal(pageHelper.getTotal());
        result.setTotalPage(pageHelper.getPages());
        result.setData(realEstates);
        return result;
    }

}

















